package com.ethank.service.DocService;

import java.io.*;
import java.util.List;

import org.apache.axis.encoding.Base64;

import javax.xml.bind.*;

/**
 * @author tf
 * @date 2024-03-08
 * @Description
 */
public class DocController {
    public static void main(String[] args) throws JAXBException {
        DocService docService = new DocService();
        DocServicePortType service = docService.getDocServiceHttpPort();
        //登陆
        String session = service.login("PDFSC", "Smsh@123..", 0, "123.234.180.90");
        //根据ID取得文档内容
        DocInfo doc = new DocInfo();
        //取得该文档的第一个附件
        DocAttachment da = new DocAttachment();

        //测试其它方法
        //
        getLink(String.valueOf(1));
         // doc = service.getDoc(2177539,session);
        //  doc = service.getDoc(2177559,session);
        // // 创建 JAXB 上下文
        // JAXBContext jaxbContext = JAXBContext.newInstance(ArrayOfDocAttachment.class);
        //
        // // 创建 Marshaller 对象
        // Marshaller marshaller = jaxbContext.createMarshaller();
        //
        // // 设置 Marshaller 的属性以格式化输出
        // marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        //
        // // 序列化 JAXBElement 到 StringWriter
        // StringWriter stringWriter = new StringWriter();
        // JAXBElement<ArrayOfDocAttachment> attachments = doc.getAttachments();
        // // ArrayOfDocAttachment value = attachments.getValue();
        //
        //
        // marshaller.marshal(attachments, stringWriter);
        //
        // // 获取生成的 XML 字符串
        // String xmlString = stringWriter.toString();
        //
        // // 输出结果
        // System.out.println(xmlString);
        //


        //得到附件内容
        //结束
        //得到附件内容
        byte[] content =  null;

        //上传附件，创建文档
        content = null;
        try {
            int byteread;
            byte data[] = new byte[1024];
            InputStream input = new FileInputStream(new File("E:\\jipiao.jpg"));

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            while ((byteread = input.read(data)) != -1) {
                out.write(data, 0, byteread);
                out.flush();
            }
            content = out.toByteArray();
            input.close();
            out.close();
        } catch(Exception e){
            e.printStackTrace();
        }
        //
        // da.setDocid(0);
        // da.setImagefileid(0);
        // da.setFilecontent(Base64.encode(content));
        // da.setFilerealpath("C:\\Users\\Administrator\\Desktop\\tmp\\invoice\\aliliushui.png");
        // da.setIszip(1);
        //
        // doc.setId(0);
        // doc.setDocSubject("test pdf1");
        // doc.setDoccontent("abcefghijk");
        // doc.setAttachments(new DocAttachment[]{da});

        da.setDocid(0);
        da.setImagefileid(0);

        ObjectFactory  objFac=new ObjectFactory();
        JAXBElement<String> filecontent = objFac.createDocAttachmentFilecontent(Base64.encode(content));
        // System.out.println("Base64.encode(content)==>"+Base64.encode(content));
        da.setFilecontent(filecontent);
        JAXBElement<String> docAttachmentFilerealpath = objFac.createDocAttachmentFilerealpath("");
        da.setFilerealpath(docAttachmentFilerealpath);
        da.setIszip(1);
        JAXBElement<String> fileName = objFac.createDocAttachmentFilename("aliliushui.png");
        da.setFilename(fileName);
        JAXBElement<String> isextfile = objFac.createDocAttachmentIsextfile("1");

        da.setIsextfile(isextfile);
        JAXBElement<String> docFileType = objFac.createDocAttachmentFiletype("2");
        JAXBElement<String> filetype = objFac.createDocAttachmentFiletype("image/png");

        da.setDocfiletype(docFileType);
        da.setFiletype(filetype);
        System.out.println("--------------444-----------" + da);
        doc.setDoccreaterid(5452);
        doc.setDoccreatertype(0);
        doc.setAccessorycount(1);
        doc.setMaincategory(41);
        doc.setSubcategory(116);
        doc.setSeccategory(117);
        doc.setOwnerid(5452);
        doc.setDocStatus(9);
        doc.setId(0);
        JAXBElement<String> docSubject = objFac.createDocInfoDocSubject("test");
        doc.setDocSubject(docSubject);
        ArrayOfDocAttachment arrayOfDocAttachment = objFac.createArrayOfDocAttachment();
        List<DocAttachment> docAttachment = arrayOfDocAttachment.getDocAttachment();
        docAttachment.add(da);
        doc.setAttachments(objFac.createDocInfoAttachments(arrayOfDocAttachment));
        int doc1 = service.createDoc(doc, session);
        System.out.println("doc1=>"+doc1);
    }

    public static String  getLink(String docId) throws JAXBException {
        DocService docService = new DocService();
        DocServicePortType service = docService.getDocServiceHttpPort();
        //登陆
        String session = service.login("PDFSC", "Smsh@123..", 0, "123.234.180.90");
        // doc = service.getDoc(2177539,session);
       DocInfo  doc = service.getDoc(2177585,session);

        // 创建 JAXB 上下文
        JAXBContext jaxbContext = JAXBContext.newInstance(ArrayOfDocAttachment.class);

        // 创建 Marshaller 对象
        Marshaller marshaller = jaxbContext.createMarshaller();

        // 设置 Marshaller 的属性以格式化输出
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        // 序列化 JAXBElement 到 StringWriter
        StringWriter stringWriter = new StringWriter();
        JAXBElement<ArrayOfDocAttachment> attachments = doc.getAttachments();
        // ArrayOfDocAttachment value = attachments.getValue();


        marshaller.marshal(attachments, stringWriter);

        // 获取生成的 XML 字符串
        String xmlString = stringWriter.toString();

        // 输出结果
        System.out.println(xmlString);

        return null;
    }
}
